import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 创建动态路由表
const asyncRoutes = [
  // 放动态路由对象
  // 组织架构
  {
    path: '/departments',
    // meta: { title: '组织架构', icon: 'departments' },
    component: Layout,
    children: [
      {
        path: '', // 如果子组件path为空, 默认自动匹配
        name: 'departments',
        component: () => import('@/views/departments/departments.vue'), // 懒加载
        meta: { title: '组织架构', icon: 'tree' }
      }
    ]
  },
  // 员工管理
  {
    path: '/employees',
    // meta: { title: '组织架构', icon: 'employees' },
    component: Layout,
    children: [
      {
        path: '', // 如果子组件path为空, 默认自动匹配
        name: 'employees',
        component: () => import('@/views/employees/employees.vue'), // 懒加载
        meta: { title: '员工管理', icon: 'user' }
      }
    ]
  },
  // 公司设置
  {
    path: '/settings',
    // meta: { title: '组织架构', icon: 'settings' },
    component: Layout,
    children: [
      {
        path: '', // 如果子组件path为空, 默认自动匹配
        name: 'settings',
        component: () => import('@/views/settings/settings.vue'), // 懒加载
        meta: { title: '公司设置', icon: 'user' }
      }
    ]
  },
  // 工资管理
  {
    path: '/salarys',
    component: Layout,
    children: [
      {
        path: '', // 如果子组件path为空, 默认自动匹配
        name: 'salarys',
        component: () => import('@/views/salarys/salarys.vue'), // 懒加载
        meta: { title: '工资管理', icon: 'money' }
      }
    ]
  }
]

//  静态路由表
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  //  最终生成的路由 (将动态路由表和静态路由表合并)
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
